<?php
/**
 * 门店数据统计表
 */

namespace app\badmin\model;


use app\common\model\BaseModel;

class CompanyShopStatistics extends BaseModel {

    /**
     * 昨日新增潜在
     * @param $params
     * @return float|string
     */
    public static function yesterdayUser($params) {
        $data = db('company_shop_statistics')
            ->whereTime('day_time', 'yesterday')
            ->where('company_shop_id', $params['company_shop_id'])
            ->value('customer_number');
        if (empty($data)) {
            return 0;
        } else {
            return $data;
        }
    }

    /**
     * 累计新增潜在
     * @return int|mixed
     */
    public static function totalUser($params) {
        $data = db('company_shop_statistics')
            ->where('company_shop_id', $params['company_shop_id'])
            ->order('day_time', 'desc')
            ->limit(0, 1)
            ->value('customer_total_number');
        if (empty($data)) {
            return 0;
        } else {
            return $data;
        }
    }

    /**
     * 新增用户折线图
     * @param $params
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function timeLine($params, $type) {
        date_default_timezone_set('PRC');

        if ($type == 1) {//潜在
            $column = 'customer_number';
        } else {//专属
            $column = 'exclusive_customer_number';
        }
        $data = [];
        $sec = CompanyStatistics::getDateFromRange($params['start_time'], $params['end_time']);
        foreach ($sec as $k => $v) {
            $num = db('company_shop_statistics')
                ->where('day_time', strtotime($v))
                ->where('company_shop_id', $params['company_shop_id'])
                ->value("$column as customer_number");
            if (empty($num)) {
                $num = 0;
            }
            $data[$k]['customer_number'] = $num;
            $date[$k] = date('m-d', strtotime($v));
        }
        return compact('data', 'date');
    }

}